package com.lsmo.android.carfans.view.favorites;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.favorites.FavoritesThread;
import com.lsmo.android.carfans.process.favorites.FavoritesProcess;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

/**
 * 经销商
 * 
 * @author maxl
 *
 */
public class FavoritesDistributorView extends LinearLayout implements
		ViewProcessCallBack {
	
	public static final String TAG = FavoritesDistributorView.class.getSimpleName();
	
	public Context mContext;
	private ListView mListView;
	private ViewGroup mProgressBarLayout;
	private FavoritesProcess mProcess;
	

	public FavoritesDistributorView(Context context) {
		super(context);
		
		this.mContext = context;
		this.mProcess = new FavoritesProcess();

		((Activity) context).getLayoutInflater().inflate(
				R.layout.favorites_layout, this);

		this.postInvalidate();
		mListView = (ListView) this.findViewById(R.id.favorites_listView);
		mProgressBarLayout = (ViewGroup) this.findViewById(R.id.loading_layout);
		updateView();
	}

	@Override
	public void processAfterTaskFinish(int code, Object result) {
		if (code == FavoritesDistributorTask.GET_DISTRIBUTOR_THREADS_LIST) {

			if (result != null) {
				mListView.setAdapter(new FavoritesDistributorListAdapter(mContext, (List<FavoritesThread.CarDistributor>)result));
				mListView.postInvalidate();
			}

		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
	}
	
	private class FavoritesDistributorListAdapter extends BaseAdapter {

		private List<FavoritesThread.CarDistributor> list;
		private Context mContext;

		public FavoritesDistributorListAdapter(Context context,
				List<FavoritesThread.CarDistributor> l) {
			this.list = l;
			this.mContext = context;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int i) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewgroup) {
			if (convertView == null) {
				convertView = new FavoritesDistributorListItemLayout(this.mContext);
			}

			((FavoritesDistributorListItemLayout) convertView).updateView(this.list
					.get(position));

			return convertView;
		}

	}
	
	private class FavoritesDistributorTask extends BaseTask {

		public static final int GET_DISTRIBUTOR_THREADS_LIST = 1; // 查询收藏的车型列表
		private FavoritesProcess mProcess;

		public FavoritesDistributorTask(int code, Context context,
				ViewProcessCallBack view, BaseProcess process) {
			super(code, context, view);
			this.mProcess = (FavoritesProcess) process;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

		}

		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;

			switch (this.mCode) {
			case GET_DISTRIBUTOR_THREADS_LIST:

				result = mProcess.getDistributorThreadsList(params);

				break;
			}
			return result;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);

			switch (this.mCode) {
				case GET_DISTRIBUTOR_THREADS_LIST:
	
					mView.processAfterTaskFinish(this.mCode, result);
	
					break;
			}
		}
	}

	@Override
	public void updateView() {
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);

		new FavoritesDistributorTask(FavoritesDistributorTask.GET_DISTRIBUTOR_THREADS_LIST,
				this.mContext, this, this.mProcess).execute(new String[] {});
	}
}